\CUSLibraryDelayedUntil{tikz}
\CUSProvideLibrary{pgf}{\cus@d@te}{\cus@versi@n}{pgf and tikz support}
\usetikzlibrary{spath3}

\ekeysdeclarecmd\pgfdeclareimagep{O{}mm}
  {\cus@getgraphicsname\@curr@file{\cus@filename{#3}}%
    \ifx\@curr@file\relax\def\@curr@file{""}\fi
    \pgf@declareimage[#1]{#2}{\@curr@file}}
\ekeysdeclarecmd\pgfimagep{&O{}m}
  {\pgfdeclareimagep#1{pgflastimage}{#2}\pgfuseimage{pgflastimage}}

% shade text, see: https://tex.stackexchange.com/questions/192496/
\newbox\cus@picturebox 
\protected\def\shadetext#1#2{%
  \setbox\cus@picturebox=\hbox{{\cus@pdfliteral{7 Tr }#2}}%
  \tikz[baseline=0,line width=0pt]\path \pgfextra{\rlap{\copy\cus@picturebox}} [#1] 
    (0,-\dp\cus@picturebox) rectangle (\wd\cus@picturebox,\ht\cus@picturebox);}
\protected\def\shadetextbox#1{%
  \@collectn@box\cus@picturebox\hbox{\shadetext{#1}{\unhbox\cus@picturebox}}} 

\newbox\cus@phantombox 
\protected\def\cus@phantomtobox#1{%
  \setbox\cus@phantombox=\null 
  \ht\cus@phantombox=\ht#1%
  \dp\cus@phantombox=\dp#1%
  \wd\cus@phantombox=\wd#1%
  \box\cus@phantombox}

\newcommand\shadecontent[3][]{% node options, shading options, content
  \setbox\cus@picturebox=\hbox{#3}%
  \begin{tikzpicture}[baseline=(textnode.base)]
    \node[shade,#2,inner sep=0pt,outer sep=0pt,#1](textnode)
      {\cus@phantomtobox\cus@picturebox};
    \begin{scope}[transparency group=knockout]
      \fill[white](textnode.south west)rectangle(textnode.north east);
      \node[opacity=0,inner sep=0pt,outer xsep=0pt,#1]{\box\cus@picturebox};
    \end{scope}
  \end{tikzpicture}% 
}
\newcommand\shadecontentbox[2][]{%
  \@collectn@box\cus@picturebox\hbox{\shadecontent[{#1}]{#2}{\unhbox\cus@picturebox}}}


\def\cus@pgf@initbfnodes{%
  \cus@pgf@initpagenode
  \cus@pgf@initlayoutnode
  \cus@pgf@inittextnode
  \cus@pgf@initmarginnode
  \cus@pgf@initheadernode
  \cus@pgf@initfooternode
}
\edef\cus@pgf@initbfnodes@#1{%
  \noexpand\@namedef{pgf@sh@ns@#1}{\@nameuse{pgf@sh@ns@current page}}%
  \noexpand\@namedef{pgf@sh@nt@#1}{\@nameuse{pgf@sh@nt@current page}}%
  \noexpand\@namedef{pgf@sh@pi@#1}{\@nameuse{pgf@sh@pi@current page}}%
  \noexpand\@namedef{pgf@sh@np@#1}}
\def\cus@pgf@initpagenode{\cus@pgf@initbfnodes@{page}{%
    \def\southwest{\pgfpointorigin}%
    \def\northeast{\pgfpoint{\paperwidth}{\paperheight}}%
  }%
  \cus@pgf@initbfnodes@{paper}{%
    \def\southwest{\pgfpointorigin}%
    \def\northeast{\pgfpoint{\paperwidth}{\paperheight}}%
  }%
}
\def\cus@pgf@initlayoutnode{\cus@pgf@initbfnodes@{layout}{%
  \def\southwest{\pgfpoint{\layoutloffset}{\the\layoutboffset}}%
  \def\northeast{\pgfpoint{\layoutloffset+\layoutwidth}{\paperheight-\layouttoffset}}%
}}
\def\cus@pgf@ifodd{\ifcase\if@twoside\ifodd\c@page 0 \else 1 \fi\else 0 \fi
  \expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi}
\def\cus@pgf@hfO#1#2{(\@nameuse{f@nch@offset@\cus@pgf@ifodd oe#1#2}+0pt)}
\def\cus@pgf@margin{\cus@pgf@ifodd\oddsidemargin\evensidemargin}
\def\cus@pgf@textleft{(1in+\hoffset+\cus@pgf@margin)}
\def\cus@pgf@texttop{(1in+\voffset+\topmargin+\headheight+\headsep)}
\def\cus@pgf@inittextnode{\cus@pgf@initbfnodes@{text}{%
  \def\southwest{\pgfpoint{\fpeval{\cus@pgf@textleft}}
    {\fpeval{\paperheight-\cus@pgf@texttop-\textheight}}}%
  \def\northeast{\pgfpoint{\fpeval{\cus@pgf@textleft+\textwidth}}
    {\fpeval{\paperheight-\cus@pgf@texttop}}}}}
\def\cus@pgf@initmarginnode{\cus@pgf@initbfnodes@{margin}{%
  \def\southwest{\pgfpoint{\fpeval{\cus@pgf@textleft\cus@pgf@ifodd{+\textwidth+\marginparsep}{-\marginparsep-\marginparwidth}}}
    {\fpeval{\paperheight-\cus@pgf@texttop-\textheight}}}%
  \def\northeast{\pgfpoint{\fpeval{\cus@pgf@textleft\cus@pgf@ifodd{+\textwidth+\marginparsep+\marginparwidth}{-\marginparsep}}}
    {\fpeval{\paperheight-\cus@pgf@texttop}}}}}
\def\cus@pgf@initheadernode{\cus@pgf@initbfnodes@{header}{%
  \def\southwest{\pgfpoint{\fpeval{\cus@pgf@textleft-\cus@pgf@hfO lh}}
    {\fpeval{\paperheight-\cus@pgf@texttop+\headsep}}}%
  \def\northeast{\pgfpoint{\fpeval{\cus@pgf@textleft+\textwidth+\cus@pgf@hfO rh}}
    {\fpeval{\paperheight-\cus@pgf@texttop+\headsep+\headheight}}}}}
\def\cus@pgf@initfooternode{\cus@pgf@initbfnodes@{footer}{%
  \def\southwest{\pgfpoint{\fpeval{\cus@pgf@textleft-\cus@pgf@hfO lf}}
    {\fpeval{\paperheight-\cus@pgf@texttop-\textheight-\footskip}}}%
  \def\northeast{\pgfpoint{\fpeval{\cus@pgf@textleft+\textwidth+\cus@pgf@hfO rf}}
    {\paperheight-\cus@pgf@texttop-\textheight}}}}
\long\@namedef{__cus_bfground_tikz:n}#1{%
  \put(0pt,-\paperheight){\hb@xt@\z@{\begin{tikzpicture}
    \cus@pgf@initbfnodes
    \path[use as bounding box](page.south west)rectangle(page.north east);
    #1\end{tikzpicture}\hss}}}


\def\cus@tikz@pathend{}
\def\cus@tmp#1\tikz@path@do@at@end#2\@nil{%
  \def\tikz@finish{#1#2\cus@tikz@pathend\tikz@path@do@at@end}}
\expandafter\cus@tmp\tikz@finish\@nil
\pgfkeys{
  /tikz/tangent/.code=\@tangent@process{#1},
  /tikz/at tangent/.style={shift=(tangent point #1),
    x=(tangent unit vector #1),y=(tangent orthogonal unit vector #1)},
  /tikz/at tangent/.default=last,
}
\ExplSyntaxOn
\cs_new:Npn \@tangent@process #1
  {
    \seq_set_split:Nnn \l__cus_tmp_seq { ~and } {#1}
    \seq_pop_left:NNT \l__cus_tmp_seq \l__cus_tmp_tl
      {
        \seq_set_split:NnV \l__cus_tmpa_seq { ~at } \l__cus_tmp_tl
        \seq_pop_left:NNT \l__cus_tmpa_seq \l__cus_tmpa_tl
          { % a=coordinate, b=pos
            \seq_pop_left:NN \l__cus_tmpa_seq \l__cus_tmpb_tl
            \quark_if_no_value:NT \l__cus_tmpb_tl
              {
                \tl_set_eq:NN \l__cus_tmpb_tl \l__cus_tmpa_tl
                \tl_set:Nn \l__cus_tmpa_tl { last }
              }
          }
        \tl_set:Nx \l__cus_tmp_tl { path~\l__cus_tmpa_tl }
        \exp_args:Ne \pgfkeysalso { spath/save = \l__cus_tmp_tl }
        \tl_gput_right:Nn \cus@tikz@pathend { \global\let\cus@tikz@pathend\@empty }
        \exp_args:Ne \@tangent@process@ 
          { \l__cus_tmpa_tl \c_space_tl at \exp_not:o \l__cus_tmpb_tl }
        \seq_map_function:NN \l__cus_tmp_seq \@tangent@process@
      }
  }
\cs_new:Npn \@tangent@process@ #1
  {
    \tl_if_in:nnTF {#1} { ~at } 
      { \@tangent@process@@ #1 \@nil }
      { \@tangent@process last~at #1 \@nil }
  }
\cs_new:Npn \@tangent@process@@ #1 ~at #2 \@nil 
  {
    \tl_gput_right:Nx \cus@tikz@pathend 
      {
        \exp_not:N \@tangent@getcoordinate {#1} { \l__cus_tmp_tl }
          { \exp_not:o { \use:n #2 } }
      }
  }
\ExplSyntaxOff
\def\@tangent@getcoordinate#1#2#3{\path
  [spath/use=#2,postaction=decorate,decoration={
    markings, mark=at position{#3} with{%
          \coordinate (tangent point #1) at (0pt,0pt);
          \coordinate (tangent unit vector #1) at (1,0pt);
          \coordinate (tangent orthogonal unit vector #1) at (0pt,1);
        }
  }];}
